﻿#include <iostream>
#include <unordered_set>
#include <unordered_map>

using namespace std;

/**
 * 给定一个数组arr，求差值为k的去重数字对。
 *
 * 比如[3,2,5,0,7,0] 找到差值为2的所有的数字对.
 * 有(0,2),(3,5),(5,7)
 */
int main_DifferenceValuePair()
{
    int arr[] = {1,2,3,4,5,6,7,8,9,10,11,13,15,7,12,19,18,21,30,31,32,33,0};
    int count = sizeof(arr) / sizeof(int);
    const int targetOffset = 2;
    unordered_set<int> numberSet;
    for (int i = 0; i < count; i++)
    {
        numberSet.insert(arr[i]);
    }

    unordered_map<int, int> numberPairMap;
    for (auto it = numberSet.begin(); it != numberSet.end(); it++)
    {
        auto cur = *it;
        auto target = numberSet.find(cur + targetOffset);
        if (target != numberSet.end())
        {
            numberPairMap.insert({cur, *target});
        }
    }

    for (auto pair = numberPairMap.begin(); pair != numberPairMap.end(); pair++)
    {
        printf("(%d,%d) ", pair->first, pair->second);
    }

    return 0;
}